package org.example.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.example.entity.Area;
import org.example.entity.Boat;
import org.example.entity.Shippinglist;
import org.example.mapper.AreaMapper;
import org.example.service.AreaService;
import org.example.service.IBoatService;
import org.example.service.IShippinglistService;
import org.example.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
@Api(tags = "水上航线管理")
@RequestMapping("/scs")
@CrossOrigin
public class BoatController {
    @Autowired
    private IBoatService boatService;
    @Autowired
    private AreaService areaService;

    @Autowired
    private IShippinglistService shippinglistService;

    //获取热门航线列表
    @ApiOperation("获取热门航线列表")
    @GetMapping("/get-hotBoat")
    public Result getHotBoat() {
        // 1. 查询所有热门航线
        QueryWrapper<Boat> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("status", 1);
        List<Boat> boatList = boatService.list(queryWrapper);

        // 3. 设置价格并填充城市名称
        boatList.forEach(boat -> {
            // 设置出发地和目的地名称
            Area startArea = areaService.getById(boat.getBeginning());
            boat.setBeginningName(startArea.getName());
            Area endArea = areaService.getById(boat.getDestination());
            boat.setDestinationName(endArea.getName());

//            // 从Map中直接取最低价格
//            Double minPrice = minPriceMap.get(boat.getId());
//            boat.setPrice(minPrice != null ? minPrice : 0.0);
        });

        return new Result("200", "成功", boatList);
    }

    //根据起点终点和时间段查询航线
    @ApiOperation("根据起点终点和时间段查询航线")
    @GetMapping("/get-boatList")
    public Result getBoat(@RequestParam String start,
                          @RequestParam String end,
                          @RequestParam( defaultValue="false") Boolean time1,
                          @RequestParam( defaultValue="false") Boolean time2,
                          @RequestParam( defaultValue="false") Boolean time3,
                          @RequestParam( defaultValue="false") Boolean time4) {
        Area name = areaService.getOne(new QueryWrapper<Area>().eq("name", start));  //获取起点id
        Area name1 = areaService.getOne(new QueryWrapper<Area>().eq("name", end));  //获取终点id
        List<Boat> boatList = boatService.getBoat(name.getId()+"", name1.getId()+"", time1, time2, time3, time4);
        return new Result("200", "成功", boatList);
    }
}
